package multithread;

//原子类
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadDemo14 {
    private static AtomicInteger count = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for(int i =0;i<50000;i++){
                //count++;
                count.getAndIncrement();
                //++count
                //count.incrementAndGet();
                //count += n
                //count.getAndAdd(n);
            }
        });

        Thread t2 = new Thread(() -> {
            for(int i =0;i<50000;i++){
                //count++;
                count.getAndIncrement();
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();

        //上述代码存在问题 输出结果不是100000
        System.out.println("count = " + count.get());
    }
}


